package com.jeesite.modules.exam.quota.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jeesite.modules.exam.quota.dto.VersionInfoDto;
import com.jeesite.modules.exam.quota.entity.DcSupExamQuotaOrg;
import com.jeesite.modules.exam.quota.entity.DcSupExamSumQuotaInfo;
import com.jeesite.modules.exam.quota.service.DcSupExamQuotaOrgService;
import com.jeesite.modules.exam.quota.service.DcSupExamSumQuotaInfoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.exam.quota.entity.DcSupExamSumQuota;
import com.jeesite.modules.exam.quota.service.DcSupExamSumQuotaService;

import java.util.List;

/**
 * 考核任务指标汇总表Controller
 * @author xhc
 * @version 2022-05-28
 */
@Controller
@RequestMapping(value = "${adminPath}/quota/dcSupExamSumQuota")
public class DcSupExamSumQuotaController extends BaseController {

	@Autowired
	private DcSupExamSumQuotaService dcSupExamSumQuotaService;
	@Autowired
	private DcSupExamQuotaOrgService dcSupExamQuotaOrgService;
	@Autowired
	private DcSupExamSumQuotaInfoService dcSupExamSumQuotaInfoService;
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public DcSupExamSumQuota get(String id, boolean isNewRecord) {
		return dcSupExamSumQuotaService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("quota:dcSupExamSumQuota:view")
	@RequestMapping(value = {"list", ""})
	public String list(DcSupExamSumQuota dcSupExamSumQuota, Model model,String examId,String examYear) {
		dcSupExamSumQuota.setRemarks2(examYear);
		dcSupExamSumQuota.setExamId(examId);
		model.addAttribute("dcSupExamSumQuota", dcSupExamSumQuota);
		List<DcSupExamQuotaOrg> list = dcSupExamQuotaOrgService.findOrgByExamId(dcSupExamSumQuota.getExamId());
		Integer isPush= dcSupExamSumQuotaInfoService.isPush(dcSupExamSumQuota.getExamId(),"2");
		model.addAttribute("assessmentUnit", list);
		model.addAttribute("isPush",isPush==null?999:isPush);
		return "modules/exam/quota/dcSupExamSumQuotaListIndex";
	}

	/**
	 * 查询指标汇总页中，左侧最新的被考核单位
	 * @param examId
	 * @return
	 */
	@RequiresPermissions("quota:dcSupExamSumQuota:view")
	@RequestMapping(value = {"getLatestAssessmentUnit", ""})
	@ResponseBody
	public String getLatestAssessmentUnit(String examId) {
		List<DcSupExamQuotaOrg> list = dcSupExamQuotaOrgService.findOrgByExamId(examId);
		return renderResult(Global.TRUE, "", list);
	}

	/**
	 * 是否存在机构未审核的单项关键指标
	 * @param model
	 * @param examId 任务id
	 * @param examYear 考核年份
	 * @return
	 */
	@RequiresPermissions("quota:dcSupExamSumQuota:view")
	@RequestMapping(value = {"isExistNoAudit", ""})
	@ResponseBody
	public String isExistNoAudit( Model model,String examId,String examYear) {
		String msg = dcSupExamSumQuotaInfoService.isExistNoAudit(examId,examYear);
		return renderResult(Global.TRUE, text(msg));
	}




	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("quota:dcSupExamSumQuota:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<DcSupExamSumQuota> listData(DcSupExamSumQuota dcSupExamSumQuota, HttpServletRequest request, HttpServletResponse response) {
		dcSupExamSumQuota.setPage(new Page<>(request, response));
		Page<DcSupExamSumQuota> page = dcSupExamSumQuotaService.findPage(dcSupExamSumQuota);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("quota:dcSupExamSumQuota:view")
	@RequestMapping(value = "form")
	public String form(DcSupExamSumQuota dcSupExamSumQuota, Model model) {
		model.addAttribute("dcSupExamSumQuota", dcSupExamSumQuota);
		return "modules/exam/quota/dcSupExamSumQuotaForm";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("quota:dcSupExamSumQuota:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated DcSupExamSumQuota dcSupExamSumQuota) {
		dcSupExamSumQuotaService.save(dcSupExamSumQuota);
		return renderResult(Global.TRUE, text("保存考核任务指标汇总表成功！"));
	}
	
	/**
	 * 停用数据
	 */
	@RequiresPermissions("quota:dcSupExamSumQuota:edit")
	@RequestMapping(value = "disable")
	@ResponseBody
	public String disable(DcSupExamSumQuota dcSupExamSumQuota) {
		dcSupExamSumQuota.setStatus(DcSupExamSumQuota.STATUS_DISABLE);
		dcSupExamSumQuotaService.updateStatus(dcSupExamSumQuota);
		return renderResult(Global.TRUE, text("停用考核任务指标汇总表成功"));
	}
	
	/**
	 * 启用数据
	 */
	@RequiresPermissions("quota:dcSupExamSumQuota:edit")
	@RequestMapping(value = "enable")
	@ResponseBody
	public String enable(DcSupExamSumQuota dcSupExamSumQuota) {
		dcSupExamSumQuota.setStatus(DcSupExamSumQuota.STATUS_NORMAL);
		dcSupExamSumQuotaService.updateStatus(dcSupExamSumQuota);
		return renderResult(Global.TRUE, text("启用考核任务指标汇总表成功"));
	}
	
	/**
	 * 删除数据
	 */
	@RequiresPermissions("quota:dcSupExamSumQuota:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(DcSupExamSumQuota dcSupExamSumQuota) {
		dcSupExamSumQuotaService.delete(dcSupExamSumQuota);
		return renderResult(Global.TRUE, text("删除考核任务指标汇总表成功！"));
	}

	// -----------------
	@RequiresPermissions("quota:dcSupExamSumQuota:view")
	@RequestMapping(value = "newIndex")
	public String newIndex(DcSupExamSumQuota dcSupExamSumQuota, Model model,String examId,String examYear) {
		dcSupExamSumQuota.setRemarks2(examYear);
		dcSupExamSumQuota.setExamId(examId);
		model.addAttribute("dcSupExamSumQuota", dcSupExamSumQuota);
		 DcSupExamSumQuotaInfo dcSupExamSumQuotaInfo = new DcSupExamSumQuotaInfo();
		dcSupExamSumQuotaInfo.setExamId(examId);
		List<VersionInfoDto> version = dcSupExamSumQuotaInfoService.queryVersion(dcSupExamSumQuotaInfo);
		if (!CollectionUtils.isEmpty(version)){
			VersionInfoDto data=new VersionInfoDto();
			data.setVersion(" ");
			data.setVersionName("初 始 版 本");
			version.add(version.size(), data);
		}
		model.addAttribute("versions",version);
		return "modules/exam/quota/quotasumchange/dcSupExamSumQuotaListPageIndex";
	}


	/**
	 * 左侧被考核机构
	 */
	@RequiresPermissions("quota:dcSupExamSumQuota:view")
	@RequestMapping(value = "leftOrgs")
	public String leftOrgs(DcSupExamSumQuota dcSupExamSumQuota, Model model,String examId,String examYear) {
		dcSupExamSumQuota.setRemarks2(examYear);
		dcSupExamSumQuota.setExamId(examId);
		model.addAttribute("dcSupExamSumQuota", dcSupExamSumQuota);
		List<DcSupExamQuotaOrg> list = dcSupExamQuotaOrgService.findOrgByExamIdAndVersion(dcSupExamSumQuota);
		model.addAttribute("assessmentUnit", list);
		return "modules/exam/quota/quotasumchange/newSumQuotaListIndex";
	}

}